import axios from 'axios'
import { getToken } from '@/libs/util'

export async function downloadFile (fileUrl, fileName) {
  if (!fileUrl) return
  let res = await axios({
    method: 'get',
    url: `${this.$computedBaseUrl}/platform-file/file/img/download/` + fileUrl,
    headers: {
      Authorization: getToken()
    },
    responseType: 'blob'
  })
  let newUrl = window.URL.createObjectURL(res.data)
  let a = document.createElement('a')
  a.href = newUrl
  a.download = fileName
  a.click()
  a.remove()

  window.URL.revokeObjectURL(newUrl)
}
